import RouteGenerator
import sampling
import sys, getpass
import datetime

"""
 interface for PEIR or other projects
 input: numRoute            - number of paths desired
        startLat, startLong - coordinates of start position
        endLat  , endLong   - coordinates of end position
        speed               - speed of vehicle or person (km/h)
        sampleRate          - number of seconds between each sample
 output: gpsTraceNoisy      - a dictionary w/ key = cost of path, value = path
         if you want the not noisy GPS trace, return gpsTrace instead
        dictionary and files return list which is a route
        the routes are in the format: lat, long, horz error, timestamp, speed, speed error

"""
def peir_interface(numRoutes, startLat, startLong, endLat, endLong, speed = 0, sampleRate = 10, start = datetime.datetime.now(), modeOfT = 'car'):
    RouteGenerator.setConfig()
    RouteGenerator.debug = False
    
    routes = RouteGenerator.generateRouteFromGPS(startLat, startLong, endLat, endLong, numRoutes, modeOfT)
    
    if routes[0] == 0:
        routes.pop(0)
        routes = routes[0]
    else:
        print "ERROR"
        exit()
    
    gpsTrace      = {}
    gpsTraceNoisy = {}
    num = 0
    base = "gpsTrace"
    finaltime = None
    if speed == 0:
        speed = RouteGenerator.getSpeedDict()[modeOfT]
    
    for result in routes:
        temp                            = sampling.getCoordinatesBetweenXandY(result.gpsCoords, speed, sampleRate, start)
        gpsTrace[result.totalCost]      = temp[0]
        gpsTraceNoisy[result.totalCost] = temp[1]
        
        f = open(base + str(num) + '.txt', 'a')
        for lat,lon,horzAcc,tim,speed,speedAcc in temp[0]:
            f.write(str(lat))
            f.write(",")
            f.write(str(lon))
            f.write(",")
            f.write(str(horzAcc))
            finaltime = str(tim) #keep track of the last time 
            f.write(",")
            f.write(str(tim))
            f.write(",")
            f.write(str(speed))
            f.write(",")
            f.write(str(speedAcc))
            f.write("\n")
        f.close()

        f = open(base + 'Noisy' + str(num) + '.txt', 'a')
        for lat,lon,horzAcc,tim,speed,speedAcc in temp[1]:
            f.write(str(lat))
            f.write(",")
            f.write(str(lon))
            f.write(",")
            f.write(str(horzAcc))
            f.write(",")
            f.write(str(tim))
            f.write(",")
            f.write(str(speed))
            f.write(",")
            f.write(str(speedAcc))
            f.write("\n")
        f.close()
        num = num + 1

    #return gpsTraceNoisy
    #for key in gpsTraceNoisy.keys():
        #print gpsTraceNoisy[key]
    return gpsTraceNoisy, finaltime


if __name__ == "__main__":
    if len(sys.argv) >= 2:
        if sys.argv[1] == '-g':
            if len(sys.argv) == 10:
                peir_interface(eval(sys.argv[2]), eval(sys.argv[3]), eval(sys.argv[4]), eval(sys.argv[5]), eval(sys.argv[6]), modeOfT = sys.argv[7], sampleRate = eval(sys.argv[8]), speed = eval(sys.argv[9]))
            elif len(sys.argv) == 9:
                peir_interface(eval(sys.argv[2]), eval(sys.argv[3]), eval(sys.argv[4]), eval(sys.argv[5]), eval(sys.argv[6]), modeOfT = sys.argv[7], sampleRate = eval(sys.argv[8]))
            elif len(sys.argv) == 8:
                peir_interface(eval(sys.argv[2]), eval(sys.argv[3]), eval(sys.argv[4]), eval(sys.argv[5]), eval(sys.argv[6]), modeOfT = sys.argv[7])
            elif len(sys.argv) == 7:
                peir_interface(eval(sys.argv[2]), eval(sys.argv[3]), eval(sys.argv[4]), eval(sys.argv[5]), eval(sys.argv[6]))
            else:
                print "BAD FORMAT \n"
                print "FORMAT: -g [number of paths] [start lat] [start long] [end lat] [end long] ['bike or car'] [ sample rate] [ speed ]OR \n"
                print "FORMAT: -g [number of paths] [start lat] [start long] [end lat] [end long] ['bike or car'] [ sample rate] OR \n"
                print "FORMAT: -g [number of paths] [start lat] [start long] [end lat] [end long] ['bike or car'] OR \n"
                print "FORMAT: -g [number of paths] [start lat] [start long] [end lat] [end long]"
        else:
            print "BAD FORMAT \n"
            print "options: \n"
            print "-g : gps \n"
    else:
        print "INCORRECT FORMAT"
        print "BAD FORMAT \n"
        print "options: \n"
        print "-g : gps \n"